#include "PreCompiled.h"
#include "PropertyView.h"
#include "PropertyEditor.h"
#include "ViewProvider.h"
#include "Document_Gui.h"
#include "Application.h"
using namespace Gui;
using namespace PropertyEditor;
PropertyView::PropertyView(QWidget* parent)
:QWidget(parent)
{
   QGridLayout* pLayout = new QGridLayout(this);
   pLayout->setMargin(0);
   pLayout->setSpacing(0);
   tab = new QTabWidget(this);
   tab->setObjectName(QString::fromUtf8("propertyTab"));
   tab->setTabPosition(QTabWidget::South);
   tab->setTabShape(QTabWidget::Triangular);
   pLayout->addWidget(tab,0,0);

   propertyEditorView = new Gui::PropertyEditor::PropertyEditor();
   propertyEditorView->setAutomaticDocumentUpdate(false);
   tab->addTab(propertyEditorView,tr("View"));
   propertyEditorData = new Gui::PropertyEditor::PropertyEditor();
   propertyEditorData->setAutomaticDocumentUpdate(true);
   tab->addTab(propertyEditorData,tr("Data"));


}

PropertyView::~PropertyView(void)
{
}

void PropertyView::onSelectionChanged(const Gui::SelectionChanges& msg)
{
    if(msg.Type != SelectionChanges::AddSelection &&
		msg.Type != SelectionChanges::RmvSelection &&
	    msg.Type != SelectionChanges::SetSelection &&
        msg.Type != SelectionChanges::ClrSelection)
		return;

	std::map<std::pair<std::string,int>,std::vector<App::Property*>>propDataMap;
	std::map<std::pair<std::string,int>,std::vector<App::Property*>>propViewMap;
	
	std::vector<SelectionSingleton::SelObj> array = Gui::Selection().getCompleteSelection();
	for(std::vector<SelectionSingleton::SelObj>::iterator it = array.begin(); it != array.end();++it)
	{
		App::DocumentObject* ob = 0;
		Gui::ViewProvider* vp = 0;

		std::map<std::string,App::Property*>dataMap;
		std::map<std::string,App::Property*>viewMap;
		if((*it).pObject)
		{
           ob = (*it).pObject;
		   ob->getPropertyMap(dataMap);

		   Document_Gui* doc = Application::Instance()->getDocument(it->pDoc);
		   vp = doc->getViewProvider((*it).pObject);
		   vp->getPropertyMap(viewMap);
		}

	std::map<std::string,App::Property*>::iterator pt;
	if (ob) {
		for (pt = dataMap.begin(); pt != dataMap.end(); ++pt) {
			std::pair<std::string, int> nameType = std::make_pair
				<std::string, int>(pt->first, pt->second->getTypeId().getKey());
			if (!ob->isHidden(pt->second) && !pt->second->StatusBits.test(3))
				propDataMap[nameType].push_back(pt->second);
		}
	}
	// the same for the view properties
	if (vp) {
		for(pt = viewMap.begin(); pt != viewMap.end(); ++pt) {
			std::pair<std::string, int> nameType = std::make_pair
				<std::string, int>( pt->first, pt->second->getTypeId().getKey());
			if (!vp->isHidden(pt->second) && !pt->second->StatusBits.test(3))
				propViewMap[nameType].push_back(pt->second);
		}
	}

	}

	std::map<std::pair<std::string,int>,std::vector<App::Property*>>::const_iterator it;
	std::map<std::string,std::vector<App::Property*>>dataProps;

	for (it = propDataMap.begin(); it != propDataMap.end(); ++it)
	{
		if(it->second.size() == array.size())
		dataProps[it->first.first] = it->second;
	}

	propertyEditorData->buildUp(dataProps);

	std::map<std::string,std::vector<App::Property*>> viewProps;
	for(it = propViewMap.begin(); it != propViewMap.end(); ++it)
	{
		if(it->second.size() == array.size())
         viewProps[it->first.first] = it->second;
	}
	propertyEditorView->buildUp(viewProps);


}
void PropertyView::changeEvent(QEvent *e)
{

}
// --------------------------------------------------------------------------------- [4/8/2013 Administrator]

PropertyDockView::PropertyDockView(QWidget* parent)
:DockWindow(parent)
{
   PropertyView* view = new PropertyView(this);
   QGridLayout* pLayout = new QGridLayout(this);

   pLayout->setSpacing(0);
   pLayout->setMargin(0);
   pLayout->addWidget(view,0,0);

   resize(200,400);

}
PropertyDockView::~PropertyDockView()
{

}

#include "moc_PropertyView.cpp"
